package org.lc.oauth.config;

import jakarta.annotation.Resource;
import org.lc.oauth.component.RedisOAuthReceiver;
import org.lc.oauth.service.Oauth2Service;
import org.lc.platform.redis.constant.RedisConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

@Configuration
public class LcOauthConfig {

    @Bean
    public MessageListenerAdapter listenerAdapter(RedisOAuthReceiver receiver, Jackson2JsonRedisSerializer<?> jackson2JsonRedisSerializer) {
        MessageListenerAdapter receiveMessage = new MessageListenerAdapter(receiver, "receiveMessage");
        receiveMessage.setSerializer(jackson2JsonRedisSerializer);
        return receiveMessage;
    }

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        // 设置连接工厂
        container.setConnectionFactory(redisConnectionFactory);
        container.addMessageListener(messageListenerAdapter, new PatternTopic(RedisConstant.LOGOUT_CHANNEL));
        return container;
    }
}
